package com.elitel.frame.base.component;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.business.dao.ext.MultipleTableServerMapper;
import com.elitel.frame.main.entity.CfgExtend;
import com.elitel.frame.main.entity.CfgExtsql;
import com.elitel.frame.main.entity.vo.PageResponseVo;

@Component("redisCachMultipleTable")
public class RedisCacheMultipleTable {

//    @Autowired
//    private RedisCacheService redisCacheService;
    @Autowired
    private MultipleTableServerMapper multipleTableServerMapper;
//    @Autowired
//    private RedisConnInfo redisConnInfo;

    /**
     * modify by guoyanfei 2019/07/17
     * 新增验证redis连通性
     *
     * @see
     */
//	public PageResponseVo multipleTableServerList(CfgExtsql cfgExtsql, String sql, String count_sql) {
//		int xx = RedisDbIndex.value;
//		PageResponseVo pageResponse = new PageResponseVo();
//		if(redisConnInfo.checkConnReids()){
//			String key = "SERVER_MULTIPLE_"+cfgExtsql.getGuid();
//			if(redisCacheService.stringExists(key, xx)){
//				String cfgStr = redisCacheService.getObject(key, xx);
//				//list = (List<Map>)JSON.parseArray(cfgStr,Map.class);
//				pageResponse = JSON.parseObject(cfgStr, PageResponseVo.class);
//			}else{
//				List<Map> list = multipleTableServerMapper.queryMultipleTBListTH(sql);
//				Long count = multipleTableServerMapper.multipleTBCount(count_sql);
//				pageResponse.setTotal(count.intValue());
//				pageResponse.setRows(list);
//				String extendStr = JSON.toJSONString(pageResponse);
//				redisCacheService.saveObject(key, extendStr, xx);
//				if(null != cfgExtsql.getCatchtime() && 0 != cfgExtsql.getCatchtime()){
//					redisCacheService.setKeyTimeOut(key, cfgExtsql.getCatchtime(), xx);
//				}
//			}
//		}else {
//			List<Map> list = multipleTableServerMapper.queryMultipleTBListTH(sql);
//			Long count = multipleTableServerMapper.multipleTBCount(count_sql);
//			pageResponse.setTotal(count.intValue());
//			pageResponse.setRows(list);
//		}
//
//		return pageResponse;
//	}
    public PageResponseVo multipleTableServerList(CfgExtsql cfgExtsql, String sql, String count_sql) {
        PageResponseVo pageResponse = new PageResponseVo();
        //设置缓存时长
        if (null != cfgExtsql.getCatchtime() && 0 != cfgExtsql.getCatchtime()) {
            Integer catchtime = cfgExtsql.getCatchtime();
            switch (catchtime) {//分钟
                case 5:
                    pageResponse = multipleTableServerList5M(cfgExtsql.getGuid(), sql, count_sql);
                    break;
                case 10:
                    pageResponse = multipleTableServerList10M(cfgExtsql.getGuid(), sql, count_sql);
                    break;
                case 30:
                    pageResponse = multipleTableServerList30M(cfgExtsql.getGuid(), sql, count_sql);
                    break;
                case 60:
                    pageResponse = multipleTableServerList60M(cfgExtsql.getGuid(), sql, count_sql);
                    break;
                case 120:
                    pageResponse = multipleTableServerList120M(cfgExtsql.getGuid(), sql, count_sql);
                    break;
                default:
                    pageResponse = quertPRV(sql, count_sql);
            }
        } else {//不缓存，则直接查询获取结果
            pageResponse = quertPRV(sql, count_sql);
        }
        return pageResponse;
    }

    @Cacheable(cacheNames = CacheConfig.TIMEOUT_5_MINUTE, key = "'SERVER_MULTIPLE_'+#mguid")
    public PageResponseVo multipleTableServerList5M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = CacheConfig.TIMEOUT_10_MINUTE, key = "'SERVER_MULTIPLE_'+#mguid")
    public PageResponseVo multipleTableServerList10M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = CacheConfig.TIMEOUT_30_MINUTE, key = "'SERVER_MULTIPLE_'+#mguid")
    public PageResponseVo multipleTableServerList30M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = CacheConfig.TIMEOUT_60_MINUTE, key = "'SERVER_MULTIPLE_'+#mguid")
    public PageResponseVo multipleTableServerList60M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = CacheConfig.TIMEOUT_120_MINUTE, key = "'SERVER_MULTIPLE_'+#mguid")
    public PageResponseVo multipleTableServerList120M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    private PageResponseVo quertPRV(String sql, String count_sql) {
        PageResponseVo pageResponse = new PageResponseVo();
        List<Map> list = multipleTableServerMapper.queryMultipleTBListTH(sql);
        Long count = multipleTableServerMapper.multipleTBCount(count_sql);
        pageResponse.setTotal(count.intValue());
        pageResponse.setRows(list);
        return pageResponse;
    }
}
